package com.ebelter.btcomlib.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.models.bluetooth.bean.SleepQuality;
import com.ebelter.btcomlib.models.bluetooth.bean.SleepUnit;
import com.ebelter.btcomlib.utils.DipUtil;

import java.util.ArrayList;
import java.util.List;


public class LineBar extends View {

    private static final String TAG = "LineBar";

    public LineBar(Context context) {
        this(context,null);
    }

    public LineBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LineBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private Paint paint;

    private int width;
    private int height;


    private List<SleepUnit> datas;

    private int shenColor;
    private int qianColor;
    private int shiColor;


    private Bitmap leftBitmap;
    private Rect leftRect;
    private RectF leftRectF;


    private Bitmap rightBitmap;
    private Rect rightRect;
    private RectF rightRectF;

    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        datas=new ArrayList<>();
        shenColor= Color.parseColor("#25d775");
        qianColor= Color.parseColor("#fbc53f");
        shiColor= Color.parseColor("#ff5593");
        leftBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.ic_linebar_left);
        rightBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.ic_linebar_right);

    }






    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width=getMeasuredWidth();
        height=getMeasuredHeight();

        leftRect=new Rect(0,0,leftBitmap.getWidth(),leftBitmap.getHeight());
        rightRect=new Rect(0,0,rightBitmap.getWidth(),rightBitmap.getHeight());
        leftRectF=new RectF(0,0,DipUtil.dip2px(getContext(),10),height);
        rightRectF=new RectF(width-DipUtil.dip2px(getContext(),10),0,width,height);


    }

    public void addOneUnit(SleepUnit sleepUnit){
        if (sleepUnit==null){
            return;
        }
        datas.add(sleepUnit);
        calculate();
        invalidate();
    }

    public void setDatas(List<SleepUnit> datas){
        this.datas=datas;
        calculate();
        invalidate();
    }



    public long timeTotal;
    private void calculate() {
        long total = 0;
        for(SleepUnit sleepUnit:datas){
            total+=sleepUnit.duration;
        }
        timeTotal=total;




    }


    int dp6 = DipUtil.dip2px(getContext(),6);

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (timeTotal>0){
            float leftFrom = dp6/2;
            for(SleepUnit sleepUnit:datas){
                sleepUnit.percentage = sleepUnit.duration*1.0 / timeTotal;

                float left = leftFrom;

                float right = (float) (leftFrom +sleepUnit.percentage*(width-dp6));


                if (sleepUnit.sleepQuality== SleepQuality.DeepSleep){
                    paint.setColor(shenColor);
                }else if (sleepUnit.sleepQuality== SleepQuality.ShallowSleep){
                    paint.setColor(qianColor);
                }else if (sleepUnit.sleepQuality== SleepQuality.Sleeplessness){
                    paint.setColor(shiColor);
                }



                canvas.drawRect(left,0,right,height,paint);

                leftFrom=right;

            }



            if (leftBitmap!=null){
                canvas.drawBitmap(leftBitmap,leftRect,leftRectF,paint);
            }

            if (rightBitmap!=null){
                canvas.drawBitmap(rightBitmap,rightRect,rightRectF,paint);
            }
        }




    }


}
